package com.xqboss.common.utils;

import cn.hutool.core.text.StrSplitter;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Slf4j
public class SocketUtil {

    private static final RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
    private static final ThreadPoolTaskExecutor threadPoolTaskExecutor = SpringUtils.getBean(ThreadPoolTaskExecutor.class);
    private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean(ScheduledExecutorService.class);

    /**
     * socket推送消息-异步
     * @param userId 用户id
     * @param message 消息类容
     * @param type 类型 InteractMsgEnum 枚举
     * @param sourceId 源id
     */
    public static void sendOneMessageAsync(String userId, String message, Integer type, Long sourceId) {
        threadPoolTaskExecutor.execute(()->sendOneMessage(userId,message,type,sourceId));
    }

    /**
     * socket推送消息-延时
     * @param userId 用户id
     * @param message 消息类容
     * @param type 类型 InteractMsgEnum 枚举
     * @param sourceId 源id
     */
    public static void sendOneMessageDelayed(String userId, String message, Integer type, Long sourceId) {
        scheduledExecutorService.schedule(()->sendOneMessage(userId,message,type,sourceId),1, TimeUnit.SECONDS);
    }

    /**
     * socket推送消息
     * @param userId 用户id
     * @param message 消息类容
     * @param type 类型 InteractMsgEnum 枚举
     * @param sourceId 源id
     */
    public static void sendOneMessage(String userId, String message, Integer type, Long sourceId) {
        log.info("【websocket消息】 单点消息userId：{}, 内容：{}, 类型：{}", userId, message, type);
        String avatar = "";
        String toUserId = "";
        if (ObjUtil.equal(type, 10) //10 点赞
                || ObjUtil.equal(type, 11)) { //11 送爱心
            List<String> result = StrSplitter.split(message, ",+", 0, true, true);
            message = result.get(0);
            avatar = result.get(1);
            toUserId = result.get(2);
        }
        String consumeHotBeans = "";
        if (ObjUtil.equal(type, 12) ){//12 提醒打卡
            List<String> result = StrSplitter.split(message, ",+", 0, true, true);
            message = result.get(0);
            if (result.size()>1) {
                consumeHotBeans = result.get(1);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("toUserId", toUserId);
//        map.put("sessionId",  session.getId());
        map.put("timestamp", LocalDateUtils.getCurrentLocalDateTime());
        map.put("type", type);
        map.put("sourceId", sourceId);
        map.put("msg", message);
        map.put("avatar", avatar);
        map.put("hotBean",consumeHotBeans);
        R ping = R.ok(map);
        String str = JSONUtil.toJsonStr(ping);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("userId", userId);
        map1.put("msg", str);
        redisCache.publish("WS:user-msg", JSONUtil.toJsonStr(map1));
    }

}
